package com.qiku.controller;

import com.qiku.entity.Article;
import com.qiku.entity.ResponseResult;
import com.qiku.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(description = "文章相关接口")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @GetMapping("/findAll")
    public List<Article> findAll(){
        List<Article> list = articleService.list();
        return list;
    }
    @GetMapping("/getById/{id}")
    public Article getById(@PathVariable Long id){
        Article article = articleService.getById(id);
        return article;
    }

    /**
     * 查询热门文章  浏览量前十的文章
     * @return
     */
    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList(){
        ResponseResult result = articleService.hotArticleList();
        return result;
    }

    /**
     * 根据分类id、分页查询
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return
     */
    @GetMapping("/articleList")
    @ApiOperation(value = "根据categoryId查询文章列表",notes = "分页查询")
    //参数描述
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value ="分类id"),
            @ApiImplicitParam(name = "pageNum",value ="页号"),
            @ApiImplicitParam(name = "pageSize",value ="每页条数")
    })
    public ResponseResult articleList(Integer pageNum,Integer pageSize,Long categoryId){
        return articleService.articleList(pageNum,pageSize,categoryId);
    }
    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getArticleDetail(@PathVariable Long id){
        return articleService.getArticleDetail(id);
    }

    /**
     * 更新浏览量
     * 思路分析：
     * 1、在应用启动的时候把博客浏览量更新到redis中
     * 2、更新浏览量时，更新redis中的数据
     * 3、每个10分钟把redis中的浏览量更新到数据库
     * 4、读取文章浏览量时从redis读取
     */
    @GetMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable Long id){
        return articleService.updateViewCount(id);
    }
}
